package com.shanxia.codec.cmd;

import com.shanxia.codec.ProtocolDataBase;
import com.shanxia.codec.enums.DecodeEnum;
import lombok.Data;

import static com.shanxia.codec.Decoder.bytesToIntLittle;

/**
 * C -> S
 * 充电桩应答服务器下发升级文件名指令
 */
@Data
public class Cmd1004 extends ProtocolDataBase {

    public static final int MIN_SIZE = 4;

    private int length;         // 允许服务发送的升级 数据报文数据长度

    public Cmd1004(){
        super.setCmd(1004);
    }

    public static Cmd1004 decodeFromBase(ProtocolDataBase protocolDataBase) {
        Cmd1004 cmd = new Cmd1004();
        cmd.copyDataFrom(protocolDataBase);
        byte[] dataField = protocolDataBase.getDataField();
        if (dataField.length < MIN_SIZE) {
            cmd.setResult(DecodeEnum.InvalidData);
            return cmd;
        }
        cmd.length = bytesToIntLittle(dataField, 0);
        return cmd;
    }

    @Override
    public String toString() {

        StringBuffer buffer = new StringBuffer();
        buffer.append("#cmd1004#").append("\n");
        buffer.append("允许服务发送的升级 数据报文数据长度:").append(length).append("\n");
        return buffer.toString();
    }
}
